/*
 * File      : application.c
 * This file is part of RT-Thread RTOS
 * COPYRIGHT (C) 2009 - 2011, RT-Thread Development Team
 *
 * The license and distribution terms for this file may be
 * found in the file LICENSE in this distribution or at
 * http://www.rt-thread.org/license/LICENSE
 *
 * Change Logs:
 * Date           Author       Notes
 * 2011-05-24     Bernard      the first version
 */

/**
 * @addtogroup FM3
 */
/*@{*/

#include <rtthread.h>
#include "board.h"

#ifdef RT_USING_DFS
/* dfs init */
#include <dfs_init.h>
/* dfs filesystem:ELM filesystem init */
#include <dfs_elm.h>
/* dfs Filesystem APIs */
#include <dfs_fs.h>
#endif

#ifdef RT_USING_LWIP
#include <lwip/sys.h>
#include <lwip/api.h>
#include <netif/ethernetif.h>

/*
FM3 EMAC
P44/PHY1_RESET
P45/PHY0_RESET
*/

#define    PHY0_RESET_LOW()     FM3_GPIO->PDOR4_f.P5 = 0;
#define    PHY0_RESET_HIGH()    FM3_GPIO->PDOR4_f.P5 = 1;

#define    PHY1_RESET_LOW()     FM3_GPIO->PDOR4_f.P4 = 0;
#define    PHY1_RESET_HIGH()    FM3_GPIO->PDOR4_f.P4 = 1;

#endif

void rt_init_thread_entry(void *parameter)
{
    /**< init led device */
    {
        extern void rt_led_hw_init(void);
        rt_led_hw_init();
    }

    /* Filesystem Initialization */
#ifdef RT_USING_DFS
    {
        /* init the device filesystem */
        dfs_init();

#ifdef RT_USING_DFS_ELMFAT
        /* init the elm chan FatFs filesystam*/
        elm_init();

        /* mount ELM FatFs on NAND flash as root directory */
        if (dfs_mount("sd0", "/", "elm", 0, 0) == 0)
        {
            rt_kprintf("File System initialized!\n");
        }
        else
            rt_kprintf("File System initialzation failed!\n");
#endif
    }
#endif

    /* LwIP Initialization */
#ifdef RT_USING_LWIP
    {
        extern void lwip_sys_init(void);
        extern void fm3_emac_hw_init(void);
        extern void set_ethernet_e_cout_clock(int is_rmii);

        eth_system_device_init();

        /**< config PHY0_RESET */
        FM3_GPIO->PFR4_f.P5 = 0;
        FM3_GPIO->DDR4_f.P5 = 1;

        /**< config PHY1_RESET */
        FM3_GPIO->PFR4_f.P4 = 0;
        FM3_GPIO->DDR4_f.P4 = 1;

        /**< RESET PHY */
        PHY0_RESET_LOW();
        PHY0_RESET_HIGH();
        rt_thread_delay(RT_TICK_PER_SECOND/100); /* delay 10ms */
        PHY1_RESET_LOW();
        PHY1_RESET_HIGH();
        rt_thread_delay(RT_TICK_PER_SECOND/100); /* delay 10ms */

        /* register ethernetif device */
        set_ethernet_e_cout_clock(1);
        fm3_emac_hw_init();


        /* init all device */
        rt_device_init_all();

        /* init lwip system */
        lwip_sys_init();
        rt_kprintf("TCP/IP initialized!\n");
    }

    /* set ip */
    {
        extern void set_if(char* netif_name, char* ip_addr, char* gw_addr, char* nm_addr);
        set_if("e0", "192.168.3.30", "192.168.4.1", "255.255.255.0");
        set_if("e1", "192.168.1.30", "192.168.1.1", "255.255.255.0");
		//set_if("e0", "0.0.0.0", "0.0.0.0", "255.255.255.0");
		//set_if("e1", "0.0.0.0", "0.0.0.0", "255.255.255.0");
    }
		
		{
			void udpsrv(void* paramemter);
			udpsrv(RT_NULL);
		}
		
#if 0
	{
		extern rt_err_t pppoe_startup(void);
		pppoe_startup();
	}
#endif
#endif
}

int rt_application_init()
{
    rt_thread_t tid;

    tid = rt_thread_create("init",
                           rt_init_thread_entry, RT_NULL,
                           2048, 8, 20);
    if (tid != RT_NULL) rt_thread_startup(tid);

    return 0;
}

/*@}*/
